package com.yang.IO;

import org.junit.Test;

import java.io.*;

/**
 * @Description: TODO
 * @Author: YangTing
 * @Date: 2024-04-27 21:28
 */

public class FileReaderWrite {
    @Test
    public void test1() throws IOException {
        File file = new File("hello.txt");
        FileReader fileReader = new FileReader(file);
        int data = fileReader.read();
        while (data != -1) {
            System.out.print((char) data);
            data = fileReader.read();
        }
    }

    @Test
    public void test2() {
        FileWriter fileWriter = null;
        try {
            File file = new File("Test1.txt");
            fileWriter = new FileWriter(file,true);
            fileWriter.write("wo ai ni\n");
            fileWriter.write("qin ai de\n");
            fileWriter.write("bao bei\n");
            fileWriter.write("小羊\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileWriter != null){
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test3() throws Exception{
        File file = new File("D:\\hello.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        int read = fileInputStream.read();
        while (read != -1){
            System.out.print((char)read);
            read = fileInputStream.read();
        }
        fileInputStream.close();
    }
}
